package com.project.running.web.Servlet;

import com.project.running.bean.VerifyCode;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/6-10-06-11:05
 * @Description：com.project.running.web.Servlet
 */

@WebServlet("/getVerifyCode")
@Slf4j
@Controller
public class VerifyCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {

            int width=200;

            int height=69;

            BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//生成对应宽高的初始图片

            String randomText = VerifyCode.drawRandomText(width,height,verifyImg);

//单独的一个类方法，出于代码复用考虑，进行了封装。

//功能是生成验证码字符并加上噪点，干扰线，返回值为验证码字符

            req.getSession().setAttribute("verifyCode", randomText);
            log.debug("verifyCode  = " + randomText);

            resp.setContentType("image/png");//必须设置响应内容类型为图片，否则前台不识别

            OutputStream os = resp.getOutputStream(); //获取文件输出流

            //ImageIO.write(verifyImg,"png",os);//输出图片流
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(resp.getOutputStream());
            encoder.encode(verifyImg);

            os.flush();

            os.close();//关闭流

        } catch (IOException e) {


            e.printStackTrace();

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
